package com.ddx.service.impl;

import com.ddx.common.enums.ErrorCode;
import com.ddx.common.exception.BusinessException;
import com.ddx.dao.ShoppingCartMapper;
import com.ddx.dao.ShoppingProductMapper;
import com.ddx.dao.ShoppingSpecMapper;
import com.ddx.dao.ShoppingStoreMapper;
import com.ddx.dto.ShoppingCartVo;
import com.ddx.pojo.ShoppingCart;
import com.ddx.pojo.ShoppingProduct;
import com.ddx.pojo.ShoppingSpec;
import com.ddx.pojo.ShoppingStore;
import com.ddx.service.IShoppingCartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2018/9/5
 */
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    private static Logger logger = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    ShoppingStoreMapper shoppingStoreMapper;
    @Autowired
    ShoppingProductMapper shoppingProductMapper;
    @Autowired
    ShoppingSpecMapper shoppingSpecMapper;
    @Value("${image_address}")
    String imageAddress;
    /**
     * 获取用户购物车
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCartVo> cartInfo(String userId) {
        logger.info("用户购物车接口入参userId={}",userId);
        ShoppingCart record=new ShoppingCart();
        record.setUserId(userId);
        List<ShoppingCart> list=shoppingCartMapper.selectGroupStore(record);
        List<ShoppingCartVo> voList=new ArrayList<>();
        //获取所有商铺
        for(ShoppingCart shoppingCart:list){
            ShoppingCartVo vo=new ShoppingCartVo();
            ShoppingStore shoppingStore=shoppingStoreMapper.selectByPrimaryKey(Integer.parseInt(shoppingCart.getStoreId()));
            vo.setStoreName(shoppingStore.getName());
            ShoppingCart cart=new ShoppingCart();
            cart.setStoreId(shoppingCart.getStoreId());
            cart.setUserId(userId);
            //获取属于该商铺的所有商品
            List<ShoppingCart> shoppingCarts=shoppingCartMapper.selectByRecord(cart);
            List<ShoppingCartVo> vos=new ArrayList<>();
            for(ShoppingCart model:shoppingCarts){
                ShoppingSpec shoppingSpec=shoppingSpecMapper.selectByPrimaryKey(Integer.parseInt(model.getSpecId()));
                ShoppingProduct shoppingProduct=shoppingProductMapper.selectByPrimaryKey(Integer.parseInt(model.getProductId()));
                ShoppingCartVo shoppingCartVo=new ShoppingCartVo();
                shoppingCartVo.setProductName(shoppingProduct.getName());
                shoppingCartVo.setNowPrice(shoppingProduct.getNowPrice());
                shoppingCartVo.setMemberPrice(shoppingSpec.getSpecPrice());
                shoppingCartVo.setProductNumber(model.getProductNumber());
                shoppingCartVo.setGold(shoppingSpec.getSpecPrice().multiply(shoppingProduct.getFeeRate()).setScale(2, BigDecimal.ROUND_DOWN));
                shoppingCartVo.setSpecId(model.getSpecId());
                shoppingCartVo.setId(model.getId());
                shoppingCartVo.setPicture(imageAddress+shoppingProduct.getPicture());
                shoppingCartVo.setSpecJson(shoppingSpec.getSpecJson());
                vos.add(shoppingCartVo);
            }
            vo.setVoList(vos);
            voList.add(vo);
        }
        return voList;
    }
    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public void addCart(ShoppingCart shoppingCart) {
        logger.info("添加购物车接口入参shoppingCart={}",shoppingCart);
        //首先查询后台购物车是否有这个产品
        ShoppingCart query=new ShoppingCart();
        query.setUserId(shoppingCart.getUserId());
        query.setProductId(shoppingCart.getProductId());
        query.setSpecId(shoppingCart.getSpecId());
        List<ShoppingCart> shoppingCarts=shoppingCartMapper.selectByRecord(query);
        if(shoppingCarts==null||shoppingCarts.size()==0) {
            ShoppingProduct shoppingProduct = shoppingProductMapper.selectByPrimaryKey(Integer.parseInt(shoppingCart.getProductId()));
            //添加店铺id
            shoppingCart.setStoreId(shoppingProduct.getShopId());
            shoppingCartMapper.insertSelective(shoppingCart);
            return;
        }
        if(shoppingCarts.size()>1){
            logger.error("购物车中同一用户同一产品，同一规格有多条记录userId={},productId={},specId={}",shoppingCart.getUserId(),shoppingCart.getProductId(),shoppingCart.getSpecId());
            throw new BusinessException(ErrorCode.BUSSINESS_ERROR_201);
        }
        //直接修改商品数量
        ShoppingCart cart=shoppingCarts.get(0);
        Integer productNumber=shoppingCart.getProductNumber();
        productNumber=productNumber+cart.getProductNumber();
        ShoppingCart update=new ShoppingCart();
        update.setId(cart.getId());
        update.setProductNumber(productNumber);
        shoppingCartMapper.updateByPrimaryKeySelective(update);

    }
    /**
     * 根据id更改商品数量
     */
    @Override
    public void updateCount(ShoppingCart shoppingCart) {
        logger.info("根据id更改购物车商品数量入参shoppingCart={}",shoppingCart);
        shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
    }
    /**
     * 删除商品
     */
    @Override
    public void deleteById(Integer id) {
        logger.info("根据id删除购物车商品id入参id={}",id);
        shoppingCartMapper.deleteByPrimaryKey(id);
    }

}
